﻿using CTM.Web.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace CTM.Web.Models.View.Org
{
    public class OrgViewModel
    {
        #region Properties

        [Key]
        public Guid Id { get; set; }

        public Nullable<System.Guid> ParentId { get; set; }

        //[Required]
        //public string Code { get; set; }

        [Required]
        public string Name { get; set; }

        public string Description { get; set; }

        public string Path { get; set; }

        public bool IsDeleted { get; set; }

        public DateTime CreatedOn { get; set; }

        [StringLength(128)]
        public string CreatedBy { get; set; }

        public DateTime? ModifiedOn { get; set; }

        [StringLength(128)]
        public string ModifiedBy { get; set; }

        #endregion

        #region Relationship
        public virtual Input.OrgType Type { get; set; }
        public virtual ApplicationUser Manager { get; set; }
        public virtual ICollection<ApplicationUser> Employees { get; set; }
        public virtual Input.Org Parent { get; set; }
        public virtual ICollection<Input.Org> Children { get; set; }
        #endregion

        public OrgViewModel()
        {
            IsDeleted = false;
            CreatedOn = DateTime.UtcNow;
            //ModifiedOn = DateTime.UtcNow;
            Employees = new HashSet<ApplicationUser>();
            Children = new HashSet<Input.Org>();
        }

        public OrgViewModel(Input.Org org)
        {
            this.Id = org.Id;
            this.ParentId = org.ParentId;
            this.Name = org.Name;
            this.Description = org.Description;
            this.Path = org.Path;
            this.IsDeleted = org.IsDeleted;
            this.CreatedOn = ApplicationHelpers.ConvertToLocalTime(org.CreatedOn);
            this.CreatedBy = org.CreatedBy;
            this.ModifiedOn = org.ModifiedOn.HasValue ? ApplicationHelpers.ConvertToLocalTime(org.ModifiedOn.Value) : org.ModifiedOn;
            this.ModifiedBy = org.ModifiedBy;
            this.Type = org.Type;
            this.Manager = org.Manager;
            this.Employees = org.Employees;
            this.Parent = org.Parent;
            this.Children = org.Children;
        }
    }
}